<template>
  <PageWrapper
    :title="`用户` + userDetail?.username + `的资料`"
    content="这是用户资料详情页面。本页面仅用于演示相同路由在tab中打开多个页面并且显示不同的数据"
    contentBackground
    @back="goBack"
  >
    <template #extra>
      <a-button type="primary" danger> 禁用账号 </a-button>
      <a-button type="primary" @click="resetToDefaultPassword"> 重置密码 </a-button>
    </template>
    <template #footer>
      <a-tabs default-active-key="detail" v-model:activeKey="currentKey">
        <a-tab-pane key="detail" tab="用户资料" />
        <a-tab-pane key="logs" tab="操作日志" />
      </a-tabs>
    </template>
    <div class="pt-4 m-4 desc-wrap">
      <template v-if="currentKey == 'detail'">
        <a-descriptions title="用户资料" bordered>
          <a-descriptions-item label="登录名">{{ userDetail?.username }}</a-descriptions-item>
          <a-descriptions-item label="用户名称">{{ userDetail?.realName }}</a-descriptions-item>
          <a-descriptions-item label="主页地址">{{ userDetail?.homePath }}</a-descriptions-item>
          <a-descriptions-item label="邮箱">{{ userDetail?.email }}</a-descriptions-item>
          <a-descriptions-item label="描述" :span="2">{{ userDetail?.desc }}</a-descriptions-item>
          <a-descriptions-item label="头像" :span="3"
            ><ImagePreview :imageList="imgList"
          /></a-descriptions-item>
        </a-descriptions>
      </template>
      <template v-if="currentKey == 'logs'">
        <UserActionLogs :userId="userId" />
      </template>
    </div>
  </PageWrapper>
</template>

<script lang="ts">
  import { defineComponent, ref, onMounted, createVNode } from 'vue';
  import { useRoute } from 'vue-router';
  import { PageWrapper } from '/@/components/Page';
  import { useGo } from '/@/hooks/web/usePage';
  import { useTabs } from '/@/hooks/web/useTabs';
  import { Tabs, Row, Col, Descriptions, DescriptionsItem, Modal } from 'ant-design-vue';
  import UserActionLogs from './userActionLogs/index.vue';
  import { getUserDetailApi, resetToDefaultPasswordApi } from '/@/api/modules/system/user';
  import { UserListItem } from '/@/api/modules/system/model/userModel';
  import { CollapseContainer } from '/@/components/Container';
  import { ImagePreview } from '/@/components/Preview/index';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { ExclamationCircleOutlined } from '@ant-design/icons-vue';
  // import { createLocalStorage } from '/@/utils/cache';
  export default defineComponent({
    name: 'UserDetail',
    components: {
      PageWrapper,
      ATabs: Tabs,
      ATabPane: Tabs.TabPane,
      ARow: Row,
      ACol: Col,
      ADescriptions: Descriptions,
      ADescriptionsItem: DescriptionsItem,
      UserActionLogs,
      CollapseContainer,
      ImagePreview,
    },
    setup() {
      const { createMessage: msg } = useMessage();

      interface ImageProps {
        src: string;
        width: string | number;
        height?: string | number;
      }
      const route = useRoute();
      const go = useGo();
      // 此处可以得到用户ID
      const userId = ref(route.params?.id);
      const userDetail = ref<null | UserListItem>(null);
      const currentKey = ref('detail');
      const imgList = ref<ImageProps[]>([]);
      const { setTitle } = useTabs();
      // TODO
      // 本页代码仅作演示，实际应当通过userId从接口获得用户的相关资料
      onMounted(() => {
        getUserDetailApi({ userId: userId.value as string })
          .then((res) => {
            userDetail.value = res;
            // 设置Tab的标题（不会影响页面标题）
            setTitle('详情：用户' + userDetail.value?.username);
            // createLocalStorage().set('userid', 121);
            // console.log(createLocalStorage().get('userid'))

            imgList.value = [{ src: userDetail.value?.avatar, width: 200, height: 200 }];
          })
          .catch(() => {});
      });

      const resetToDefaultPassword = () => {
        Modal.confirm({
          title: '确定要重置此用户密码?',
          icon: createVNode(ExclamationCircleOutlined),
          async onOk() {
            try {
              msg.loading({ content: '正在重置用户密码...', duration: 0, key: 'saving' });
              return resetToDefaultPasswordApi({ userId: userId.value as string }).then(() => {
                msg.success({ content: '用户密码已重置', key: 'saving' });
              });
            } catch {
              return msg.error({ content: '重置失败', key: 'saving' });
            }
          },
          onCancel() {},
        });
      };
      // 页面左侧点击返回链接时的操作
      function goBack() {
        // 本例的效果时点击返回始终跳转到账号列表页，实际应用时可返回上一页
        go('/system/user');
      }
      return { userId, currentKey, goBack, userDetail, imgList, resetToDefaultPassword };
    },
  });
</script>

<style></style>
